package com.ry.es.client;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by walker on 17-11-22.
 *
 */
public class EsClient
{
    private static final int PORT = 9300;
    private static TransportClient client;

    public EsClient(String clusterName, String ip){

        Settings settings = Settings.builder()
                .put("cluster.name",clusterName)
                .put("client.transport.sniff", false)
                .build();
        try
        {
//            client = TransportClient.builder().settings(settings).build()
//                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), PORT));
            client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName(ip), 9300));
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
    }

    public TransportClient getClient()
    {
        return client;
    }


}
